<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("components4phpfull/jtdivwindow.inc.php");
use_unit("components4phpfull/jtgroupbox.inc.php");
use_unit("comctrls.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class Vehicle_Emission extends Page
{
       public $lblfee = null;
       public $JSValidate = null;
       public $hfError = null;
       public $lblcert = null;
       public $lblinspect = null;
       public $Label12 = null;
       public $lblcomments = null;
       public $Memocomments = null;
       public $btnShowAccount = null;
       public $cmbaccount = null;
       public $lblaccount = null;
       public $chknox = null;
       public $chkco = null;
       public $chkhc = null;
       public $chkco2 = null;
       public $ednox = null;
       public $lblnox = null;
       public $edco = null;
       public $lblco = null;
       public $edhc = null;
       public $lblhc = null;
       public $edco2 = null;
       public $lblco2 = null;
       public $chkrecordexpense = null;
       public $edtestfee = null;
       public $lbltestfee = null;
       public $edcertnum = null;
       public $lblcertnum = null;
       public $edinspectedby = null;
       public $lblinspectedby = null;
       public $dpvalidity = null;
       public $lblvalidthru = null;
       public $dpinspection = null;
       public $lblinspection = null;
       public $btnCancel = null;
       public $btnSubmit = null;
       public $JTJavaScript3 = null;
       public $JTJavaScript2 = null;
       public $JTJavaScript1 = null;
       public $compid = null;
       public $JTSiteTheme1 = null;
       public $JTDivAccount = null;
       public $JTGroupBox2 = null;
       public $btnAddAccount = null;
       public $btnDelAccount = null;
       public $edAccount = null;
       public $listAccount = null;
       public $imgBack = null;
       public $Image3 = null;
       public $hiddenId = null;
       function edtestfeeJSKeyUp($sender, $params)
       {

       ?>
       //Add your javascript code here
       //accept only numbers
       var numero = document.getElementById("edtestfee").value;
       var first = numero.indexOf('.');
       var last = numero.lastIndexOf('.');

       while(isNaN(numero))
       {
        haba = numero.length;
        haba = haba - 1;
        numero = numero.substr(0,haba);
       }

       while(first!=last)
       {
        numero = numero.slice(0,last);
        first = numero.indexOf('.');
        last = numero.lastIndexOf('.');
       }
       document.getElementById("edtestfee").value = numero;
       <?php

       }

       function edtestfeeJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edtestfee','lblfee');
       <?php

       }

       function edcertnumJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edcertnum','lblcert');
       <?php

       }

       function edinspectedbyJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edinspectedby','lblinspect');
       <?php

       }

       function Vehicle_EmissionJSLoad($sender, $params)
       {
       $this->clr_em();
       ?>
       //Add your javascript code here

       <?php

       }


       function btnDelAccountJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var AccountIndex = document.getElementById('listAccount').selectedIndex;
       var AccountValue = document.getElementById('listAccount')[AccountIndex].value;
       var comp = document.getElementById('compid').value;
       AjaxAccount3(AccountValue,comp);
       <?php

       }


       function btnAddAccountJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var addaccount = document.getElementById('edAccount').value;
       var compider = document.getElementById('compid').value;
       AjaxAccount2(addaccount,compider);
       document.Vehicle_Emission.edAccount.value = "";
       <?php

       }


       function btnShowAccountJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('JTDivAccount').Show();
       document.Vehicle_Emission.edAccount.value = "";
       <?php

       }

       function imgBackJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back2.png"
       <?php

       }

       function imgBackJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back.png"
       <?php

       }

       function btnCancelClick($sender, $params)
       {
       redirect('details_emission2.php?veh='.$this->hiddenId->getValue());


       }

       function btnSubmitClick($sender, $params)
       {
       $this->validateRequired($this->edinspectedby->Text, 'lblinspect');
       $this->validateRequired($this->edcertnum->Text, 'lblcert');
       $this->validateRequired($this->edtestfee->Text, 'lblfee');
       $error = $this->hfError->getValue();
       if($error == '1' || $number != $key)
       {
          $this->hfError->setValue('0');
          return false;
       }
       else if($error == '0')
       {

       $veh_id = $this->hiddenId->getValue();

       if ($this->chkrecordexpense->Checked==true)
       {
       $chkrecordexpense ='1';
       }
       else
       {
       $chkrecordexpense='0';
       }

       if ($this->chkco2->Checked==true)
       {
       $chkco2 ='1';
       }
       else
       {
       $chkco2 ='0';
       }

       if ($this->chkhc->Checked==true)
       {
       $chkhc ='1';
       }
       else
       {
       $chkhc ='0';
       }

       if ($this->chkco->Checked==true)
       {
       $chkco ='1';
       }
       else
       {
       $chkco ='0';
       }

       if ($this->chknox->Checked==true)
       {
       $chknox ='1';
       }
       else
       {
       $chknox ='0';
       }

       //$test_fee = $this->edtestfee->Text;
       //$test_f = $this->commafy($test_fee);

       $result = mysql_query("insert into veh_emission_tbl (vehicle_id,
       company_id,
       inspection_date,
       validity_date,
       inspector,
       cert_num,
       test_fee,
       record_expense,
       account,
       comments,
       co2,
       hc,
       co,
       nox,
       check_co2,
       check_hc,
       check_co,
       check_nox)

       values ('".$veh_id."',
       '".$_SESSION['company_id']."',
       '".$this->dpinspection->Text."',
       '".$this->dpvalidity->Text."',
       '".$this->edinspectedby->Text."',
       '".$this->edcertnum->Text."',
       '".$this->edtestfee->Text."',
       '".$chkrecordexpense."',
       '".$this->cmbaccount->getItemIndex()."',
       '".$this->Memocomments->Text."',
       '".$this->edco2->Text."',
       '".$this->edhc->Text."',
       '".$this->edco->Text."',
       '".$this->ednox->Text."',
       '".$chkco2."',
       '".$chkhc."',
       '".$chkco."',
       '".$chknox."')");

       //redirect('details_emission2.php?veh='.$veh_id);
       echo "<script type='text/javascript'>
       var answer = confirm('Add another record?');
       if(answer)
       {
       window.location = 'veh_emission_create.php?veh=".$veh_id."';
       }
       else
       {
       window.location = 'details_emission2.php?veh=".$veh_id."';
       }
       </script>";
       }
       }


       function commafy($_)
       {
        return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,' , strrev( $_ ) ) );
       }

       function clr_em()
       {
       $this->dpinspection->Text= date('Y-m-d');
       $this->dpvalidity->Text= date('Y-m-d');
       $this->edinspectedby->Text= '';
       $this->edcertnum->Text= '';
       $this->edtestfee->Text= '';
       $this->Memocomments->Text="&nbsp";
       $this->edco2->Text= '';
       $this->edhc->Text= '';
       $this->edco->Text= '';
       $this->ednox->Text= '';

       $this->cmbaccount->Clear();
       $this->cmbaccount->AddItem('--Select--', null, null);
       $this->cmbaccount->setItemIndex('--Select--', null, null);
       $result8 = mysql_query("select * from lookup_expense_tbl where company_id = '".$_SESSION['company_id']."' AND category='accounts'");

       while($row8 = mysql_fetch_array($result8))
        {
            $this->cmbaccount->AddItem($row8['value'], null, $row8['value']);
        }

       }

       function validateRequired($value, $lbl)
       {
        if($value=='' || $value==null)
        {
         $this->$lbl->Caption = '&darr; This field is required &darr;';
         $this->hfError->setValue('1');
        }
       }

       function Vehicle_EmissionCreate($sender, $params)
       {
       $this->clr_em();

       $this->compid->Value  = $_SESSION['company_id'];
       $id = $_GET['veh'];
       $this->hiddenId->setValue($id);

       $this->hfError->setValue('0');
       $this->lblinspect->Caption = '';
       $this->lblcert->Caption = '';
       $this->lblfee->Caption = '';

       $this->chknox->Checked=false;
       $this->chkco->Checked=false;
       $this->chkhc->Checked=false;
       $this->chkco2->Checked=false;
       $this->chkrecordexpense->Checked=false;

       $this->cmbaccount->Clear();
       $this->cmbaccount->AddItem('--Select--', null, null);
       $result8 = mysql_query("select * from lookup_expense_tbl where company_id = '".$_SESSION['company_id']."' AND category='accounts'");

       while($row8 = mysql_fetch_array($result8))
        {
            $this->cmbaccount->AddItem($row8['value'], null, $row8['value']);
        }

       //list account
       $this->listAccount->Clear();
       $result_a = mysql_query("select * from lookup_expense_tbl where company_id = '".$_SESSION['company_id']."' AND category='accounts'");

       while($row_a = mysql_fetch_array($result_a))
        {
            $this->listAccount->AddItem($row_a['value'], null, $row_a['value']);
        }

       $this->dpinspection->Text = date('Y-m-d');
       $this->dpvalidity->Text = date('Y-m-d');
       $this->edinspectedby->Text="";
       $this->edcertnum->Text="";
       $this->edtestfee->Text="";
       $this->Memocomments->Text="&nbsp";
       $this->edco2->Text="";
       $this->edhc->Text="";
       $this->edco->Text="";
       $this->ednox->Text="";

       $this->imgBack->setLink('details_emission2.php?veh='.$this->hiddenId->getValue());
       }



}

global $application;

global $Vehicle_Emission;

//Creates the form
$Vehicle_Emission=new Vehicle_Emission($application);

//Read from resource file
$Vehicle_Emission->loadResource(__FILE__);

//Shows the form
$Vehicle_Emission->show();

?>